﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ProjectN.Debug
{
    public partial class DebugOutputBox : Form, IDebugOutput
    {
        public DebugOutputBox()
        {
            InitializeComponent();
        }

        public static DebugOutputBox createFrom(Form form)
        {
            DebugOutputBox dbgbox = new DebugOutputBox();
            dbgbox.Owner = form;
            dbgbox.Show();
            return dbgbox;
        }

        protected void PushTime()
        {
            this.Console.Text += System.DateTime.Now.ToString("[yy-MM-dd][HH:mm:ss]");
        }
        public void Push(string str)
        {
            if (str == null) return;
            this.PushTime();
            this.Console.Text += str;
        }
        public void Pushln(string str)
        {
            if (str == null) return;
            this.PushTime();
            this.Console.Text += str;
            this.Console.Text += "\r\n";
        }
        public void PushF(string format, params object[] args)
        {
            this.Push(String.Format(format, args));
        }
        public void PushFln(string format, params object[] args)
        {
            this.Pushln(String.Format(format, args));
        }

        private void DebugOutputBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult res = MessageBox.Show(this, "关闭调试窗口?", "关闭确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (res == DialogResult.Cancel)
                e.Cancel = true;
        }

        private void DebugOutputBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.IsActive = false;
        }

        public bool IsActive { get; private set; } = true;

        public new Font Font
        {
            get { return ProjectN.Debug.Properties.Settings.Default.ConsoleFont; }
            set
            {
                base.Font = value;
                Console.Font = value;
                ProjectN.Debug.Properties.Settings.Default.ConsoleFont = value;
            }
        }

        public new Color ForeColor
        {
            get { return ProjectN.Debug.Properties.Settings.Default.ConsoleFtColor; }
            set
            {
                base.ForeColor = value;
                Console.ForeColor = value;
                ProjectN.Debug.Properties.Settings.Default.ConsoleFtColor = value;
            }
        }

        public new Color BackColor
        {
            get { return ProjectN.Debug.Properties.Settings.Default.ConsoleBgColor; }
            set
            {
                base.ForeColor = value;
                Console.BackColor = value;
                ProjectN.Debug.Properties.Settings.Default.ConsoleBgColor = value;
            }
        }

        private void Console_TextChanged(object sender, EventArgs e)
        {
            this.Console.SelectionStart = this.Console.TextLength;
            this.Console.ScrollToCaret();
        }

        private void TsmiOCFont_Click(object sender, EventArgs e)
        {
            this.FontDialog.Font = this.Font;
            this.FontDialog.Color = this.ForeColor;
            this.FontDialog.ShowDialog();
            this.Font = this.FontDialog.Font;
            this.ForeColor = this.FontDialog.Color;
        }

        private void TsmiOCBgColor_Click(object sender, EventArgs e)
        {
            this.ColorDialog.Color = this.BackColor;
            this.ColorDialog.ShowDialog();
            this.BackColor = this.ColorDialog.Color;
        }
    }
}
